package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加商品至购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 购物车加购的商品为菜品或套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //在购物车表中查询符合条件的数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 判断购物车内是否存在准备添加的商品
        if (list != null && list.size()>0){
            // 添加购物车内已有商品时，数量加一
            //查询时匹配了dishId或setmealId，故list如果存在的话只会为准备添加的商品
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //更新数据
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            // 不存在，插入购物车数据
            // 判断加购的商品是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null){
                //添加到购物车的商品为菜品
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //添加到购物车的商品为套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //无论是菜品还是套餐，number和createTime都是相同的
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //插入数据到数据库
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取当前用户id
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查看购物车
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 减少商品数量
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询购物车中商品id
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //查询时匹配了dishId或setmealId
        shoppingCart = list.get(0);
        //数量减一
        // 如果数量等于1，则直接删除商品(数量一定为大于0的整数)
        if (shoppingCart.getNumber() == 1){
            shoppingCartMapper.deleteById(shoppingCart);
        }else {
            //数量大于1，则数量减1
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //清空购物车
        shoppingCartMapper.deleteByUserId(shoppingCart);
//        //使用已创建的SQL语句
//        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
//        for (ShoppingCart cart : list) {
//            shoppingCartMapper.deleteByUserId(cart);
//        }
    }
}
